package org.skydiveapp;

import org.skydiveapplibrary.data.Constants;
import org.skydiveapplibrary.repository.SettingsRepository;
import org.skydiveapplibrary.ui.InterfaceManager;
import org.skydiveapplibrary.R;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;

import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;

public class NotesEditActivity extends SherlockActivity
{
	private boolean isDualDisplay;
	private boolean useLightTheme;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		SettingsRepository settingsRepository = new SettingsRepository(this);
		useLightTheme = settingsRepository.getUseLightTheme();
		if (useLightTheme)
			super.setTheme(R.style.SkydiveAppTheme_Light_Transparent);
		
		super.onCreate(savedInstanceState);
        
        // init ui
        initializeUI();
	}
	
	private void initializeUI()
    {
		// Theme must be set before setContentView
		Bundle extras = getIntent().getExtras();
		// set notes
		String notes = "";
		if (extras != null)
		{
			notes = (extras.getString(Constants.EXTRA_NOTES));

			if (extras.containsKey(Constants.EXTRA_SEMI_TRANSPARENT_THEME))
			{
				if (useLightTheme)
					setTheme(R.style.SkydiveAppTheme_Light_SemiTransparent);
				else
					setTheme(R.style.SkydiveAppTheme_Dark_SemiTransparent);
			}
		}
		
		// show view
		setContentView(R.layout.notes_view);
		
		isDualDisplay = this.findViewById(R.id.phoneLayout) == null;
		
		// set header text
		InterfaceManager.getInstance().setSubtitleText(getSupportActionBar(), 
						getResources().getString(R.string.notes_editor_notes_label));
		
		EditText nField = (EditText)findViewById(R.id.NotesField);
		nField.setText(notes);
    }
	
	private void save()
    {
            // get notes
            EditText nField = (EditText)findViewById(R.id.NotesField);
            String notes = nField.getText().toString();

            // set result
            Intent intent =  new Intent();
            intent.putExtra(Constants.EXTRA_NOTES, notes);
            setResult(RESULT_OK, intent);
            close();
    }
	
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	MenuInflater inflater = getSupportMenuInflater();
    	inflater.inflate(R.menu.menu_save, menu);
    	inflater.inflate(R.menu.menu_back, menu);
    	return  true;
    }
    
    @Override
    public boolean onPrepareOptionsMenu(Menu menu)
    {
    	menu.findItem(R.id.menu_back).setVisible(isDualDisplay);
    	return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int itemId = item.getItemId();
		if (itemId == R.id.menu_save) {
			save();
			return true;
		}
		else if (itemId == R.id.menu_back)
		{
			close();
			return true;
		}
		else
		{
			return super.onOptionsItemSelected(item);
		}
    }
    
	@Override
    public void onBackPressed() {
		save();
		close();
    }
	
	private void close()
	{
		finish();
	}
}
